//
//  AppendFishpondFooterView.m
//  YuTang
//
//  Created by 葛悦彣 on 2024/7/22.
//

#import "AppendFishpondFooterView.h"
#import "YTBaseSelectStringView.h"
#import "YTBaseEditorStringView.h"
#import "FishPondPhoneView.h"

@interface AppendFishpondFooterView ()

@property (nonatomic ,strong) YTBaseEditorStringView * expectYield;
@property (nonatomic ,strong) YTBaseSelectStringView * breedTypeId;
@property (nonatomic ,strong) YTBaseEditorStringView * breedCycle;

@property (nonatomic ,strong) YTBaseSelectStringView * directorName;
@property (nonatomic ,strong) YTBaseEditorStringView * area;
@property (nonatomic ,strong) FishPondPhoneView * onephone;
@property (nonatomic ,strong) FishPondPhoneView * twephone;
@property (nonatomic ,strong) FishPondPhoneView * threephone;

@property (nonatomic ,strong) YTBaseSelectStringView * templateName;
@property (nonatomic ,strong) YTBaseSelectStringView * batchFlag;
@property (nonatomic ,strong) YTBaseSelectStringView * executeDate;

@end
@implementation AppendFishpondFooterView

+ (instancetype)NewView{
    AppendFishpondFooterView * view = [AppendFishpondFooterView new];
    view.backgroundColor = UIColor.whiteColor;
    view.clipsToBounds = YES;
    [view setupView];
    return view;
}

- (void)setupView{
   
    
    
    MJWeakSelf
    YTBaseEditorStringView * expectYield = [YTBaseEditorStringView NewView];
    self.expectYield = expectYield;
    expectYield.forceFlag = NO;
    expectYield.keyboardType = UIKeyboardTypeDecimalPad;
    expectYield.name = @"预计产量(斤)";
    expectYield.block = ^(NSString * _Nonnull content) {
        weakSelf.model.addPondModelList.firstObject.expectYield = content;
    };
    [self addSubview:expectYield];
    [expectYield mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(kwt(-10));
        make.left.offset(kwt(10));        make.height.offset(kwt(60));
        make.top.offset(0);
    }];
    
    YTBaseSelectStringView * breedTypeId = [YTBaseSelectStringView NewView];
    self.breedTypeId = breedTypeId;
    breedTypeId.forceFlag = YES;
    breedTypeId.name = @"养殖种类";
    breedTypeId.block = ^(NSString * _Nonnull content) {
        if(weakSelf.openYZZLBlock){
            weakSelf.openYZZLBlock(weakSelf.model);
        }
    };
    [self addSubview:breedTypeId];
    [breedTypeId mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(kwt(-10));
        make.left.offset(kwt(10));
        make.height.offset(kwt(60));
        make.top.equalTo(expectYield.mas_bottom);
    }];
    YTBaseEditorStringView * breedCycle = [YTBaseEditorStringView NewView];
    self.breedCycle = breedCycle;
    breedCycle.forceFlag = YES;
    breedCycle.name = @"养殖周期(天)";
    breedCycle.block = ^(NSString * _Nonnull content) {
        weakSelf.model.breedCycle = content ;
    };
    breedCycle.keyboardType = UIKeyboardTypeNumberPad;
    [self addSubview:breedCycle];
    [breedCycle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(kwt(-10));
        make.left.offset(kwt(10));        make.height.offset(kwt(60));
        make.top.equalTo(breedTypeId.mas_bottom);
    }];
    
    
    YTBaseSelectStringView * directorName = [YTBaseSelectStringView NewView];
    self.directorName = directorName;
    directorName.forceFlag = YES;
    directorName.name = @"负责人";
    directorName.block = ^(NSString * _Nonnull content) {
        [weakSelf openFZR];
    };
    [self addSubview:directorName];
    [directorName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(kwt(-10));
        make.left.offset(kwt(10));
        make.top.equalTo(breedCycle.mas_bottom);
        make.height.offset(kwt(60));
    }];
    
    YTBaseEditorStringView * area = [YTBaseEditorStringView NewView];
    self.area = area;
    area.forceFlag = YES;
    area.name = @"池塘面积(亩)";
    area.keyboardType = UIKeyboardTypeNumberPad;
    area.block = ^(NSString * _Nonnull content) {
        weakSelf.model.area = content;
    };
    [self addSubview:area];
    [area mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(kwt(-10));
        make.left.offset(kwt(10));
        make.top.equalTo(directorName.mas_bottom);
        make.height.offset(kwt(60));
    }];
    
    UIView * phoneview = [UIView new];
    phoneview.backgroundColor = KcolorHex(@"#F6F7F9");
    [DTKTool YJ:kwt(5) andView:phoneview];
    [self addSubview:phoneview];
    [phoneview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(kwt(-10));
        make.left.offset(kwt(10));
        make.top.equalTo(area.mas_bottom).offset(kwt(10));
        make.height.offset(kwt(145));
    }];
    
    UILabel * phoneLab = [DTKTool labelWithText:@"*" andFont:kCFont(17) andTextColor:KcolorHex(@"#FA0F0F")];
    phoneLab.attributedText = [DTKTool AttributedStringWithStr:@"*池塘照片" andNodeColor:KcolorHex(@"#222222") andSeledColor:KcolorHex(@"#FA0F0F") andFont:kCFont(17) andPattenr:@"[*]"];
    [phoneview addSubview:phoneLab];
    [phoneLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(10));
        make.top.offset(kwt(16));
    }];
    
    FishPondPhoneView * onephone = [FishPondPhoneView NewView];
    self.onephone = onephone;
    onephone.deleteBlock = ^{
        [weakSelf deleteImgWithIndex:0];
    };
    onephone.addImgBlock = ^{
        [weakSelf addImage];
    };
    onephone.openImgBlock = ^{
        [weakSelf openImageWithIndex:0];
    };
    [onephone setContent:@"还可添加3张"];
    [phoneview addSubview:onephone];
    [onephone mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(10));
        make.top.offset(kwt(48));
        make.width.offset(kwt(105));
        make.height.offset(kwt(82));
    }];
    
    
    FishPondPhoneView * twephone = [FishPondPhoneView NewView];
    self.twephone = twephone;
    [twephone setContent:@"还可添加2张"];
    twephone.deleteBlock = ^{
        [weakSelf deleteImgWithIndex:1];
    };
    twephone.addImgBlock = ^{
        [weakSelf addImage];
    };
    twephone.openImgBlock = ^{
        [weakSelf openImageWithIndex:1];
    };
    [phoneview addSubview:twephone];
    [twephone mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(onephone.mas_right).offset(kwt(5));
        make.top.offset(kwt(48));
        make.width.offset(kwt(105));
        make.height.offset(kwt(82));
    }];
    
    FishPondPhoneView * threephone = [FishPondPhoneView NewView];
    self.threephone = threephone;
    [threephone setContent:@"还可添加1张"];
    threephone.deleteBlock = ^{
        [weakSelf deleteImgWithIndex:2];
    };
    threephone.addImgBlock = ^{
        [weakSelf addImage];
    };
    threephone.openImgBlock = ^{
        [weakSelf openImageWithIndex:2];
    };
    [phoneview addSubview:threephone];
    [threephone mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(twephone.mas_right).offset(kwt(5));
        make.top.offset(kwt(48));
        make.width.offset(kwt(105));
        make.height.offset(kwt(82));
    }];
    
    UIView * line = [UIView new];
    line.backgroundColor = KcolorHex(@"#ECEDEE");
    [self addSubview: line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.equalTo(phoneview.mas_bottom).offset(kwt(12));
        make.height.offset(kwt(10));
    }];
    
    YTBaseSelectStringView * templateName = [YTBaseSelectStringView NewView];
    self.templateName = templateName;
    templateName.forceFlag = YES;
    templateName.name = @"工作模版";
    [self addSubview:templateName];
    templateName.block = ^(NSString * _Nonnull content) {
        [weakSelf openGZMB];
    };
    [templateName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(kwt(-10));
        make.left.offset(kwt(10));
        make.top.equalTo(line.mas_bottom).offset(kwt(5));
        make.height.offset(kwt(60));
    }];
    YTBaseSelectStringView * batchFlag = [YTBaseSelectStringView NewView];
    self.batchFlag = batchFlag;
    batchFlag.content = @"否";
    batchFlag.forceFlag = YES;
    batchFlag.name = @"是否允许批量打卡";
    batchFlag.block = ^(NSString * _Nonnull content) {
        [weakSelf openPLDK];
    };
    batchFlag.NOClick = YES;
    [self addSubview:batchFlag];
    [batchFlag mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(kwt(-10));
        make.left.offset(kwt(10));      
        make.top.equalTo(templateName.mas_bottom);
        make.height.offset(kwt(60));
    }];
    YTBaseSelectStringView * executeDate = [YTBaseSelectStringView NewView];
    self.executeDate = executeDate;
    executeDate.forceFlag = YES;
    executeDate.name = @"工作首次执行日期";
    executeDate.block = ^(NSString * _Nonnull content) {
        [weakSelf openGZSJ];
    };
    [self addSubview:executeDate];
    [executeDate mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(kwt(-10));
        make.left.offset(kwt(10));
        make.top.equalTo(batchFlag.mas_bottom);
        make.height.offset(kwt(60));
    }];
}

- (void)openFZR{
    if(self.openFZRBlock){
        self.openFZRBlock(self.model);
    }
}
- (void)openGZMB{
    if(self.openGZMBBlock){
        self.openGZMBBlock(self.model);
    }
}
- (void)openPLDK{
    if(self.openPLDKBlock){
        self.openPLDKBlock(self.model);
    }
}
- (void)openGZSJ{
    if(self.openGZSJBlock){
        self.openGZSJBlock(self.model);
    }
}
- (void)deleteImgWithIndex:(int)i{
    if(self.deleteImageBlock){
        self.deleteImageBlock(self.model, i);
    }
}
- (void)addImage{
    if(self.openAddImageBlock){
        self.openAddImageBlock(self.model);
    }
}
- (void)openImageWithIndex:(int)i{
    if(self.openImageBlock){
        self.openImageBlock(self.model, i);
    }
}

- (void)setModel:(AddPondModel *)model {
    _model = model;
    
    if(self.model.addPondModelList.count > 1){
        [self.expectYield mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.offset(0);
        }];
        [self.breedTypeId mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.offset(kwt(60));
        }];
        [self.breedCycle mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.offset(kwt(60));
        }];
    } else {
        if(self.model.isYSL){///衍生类
            [self.expectYield mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.offset(0);
            }];
            [self.breedTypeId mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.offset(0);
            }];
            [self.breedCycle mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.offset(0);
            }];
        } else {
            [self.expectYield mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.offset(kwt(60));
            }];
            [self.breedTypeId mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.offset(kwt(60));
            }];
            [self.breedCycle mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.offset(kwt(60));
            }];
        }
    }
    
    
    self.expectYield.content =  model.addPondModelList.firstObject.expectYield;
    self.breedTypeId.content = model.breedTypeName;
    self.breedCycle.content =  model.breedCycle;
    self.directorName.content = model.directorName;
    self.area.content = model.area;
    int i = 0;
    for (FishPondPhoneView * phone in @[self.onephone,self.twephone,self.threephone,]) {
        if(model.pictureImgArray.count > i){
            [phone setImgUrl:model.pictureImgArray[i]];
            phone.hidden = NO;
        } else if(model.pictureImgArray.count == i){
            [phone deleteImg];
            phone.hidden = NO;
        } else {
            phone.hidden = YES;
        }
        i++;
    }
    self.templateName.content = self.model.templateName;
    self.batchFlag.content = (self.model.batchFlag == 1)?@"是":@"否";
    self.executeDate.content = self.model.executeDate;
}
@end
